package com.encyclopedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.encyclopedia.config.PageResult;
import com.encyclopedia.domain.*;
import com.encyclopedia.domain.vo.PlanVo;
import com.encyclopedia.domain.vo.PlantInfoVo;
import com.encyclopedia.mapper.*;
import com.encyclopedia.result.JsonRequest;
import com.encyclopedia.result.exception.ServiceException;
import com.encyclopedia.result.exenum.RequestException;
import com.encyclopedia.service.AdminPlantService;
import com.encyclopedia.service.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author MaLiang
 * @version 1.0
 * @date 2024-04-28 19:40
 */
@Service
@Slf4j
public class AdminPlantServiceImpl implements AdminPlantService {

    @Autowired
    private AdminPlantMapper adminPlantMapper;

    @Autowired
    private PlantBreedMapper plantBreedMapper;

    @Autowired
    private PlantDatasourceMapper plantDatasourceMapper;

    @Autowired
    private PlantEcotopeMapper plantEcotopeMapper;

    @Autowired
    private PlantGenusMapper genusMapper;

    @Autowired
    private PlantFamilyMapper plantFamilyMapper;

    @Autowired
    private PlantGrowthCharacteristicMapper plantGrowthCharacteristicMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public PageResult PlantList(Integer pageNum, Integer pageSize, String commonName, String scientificName) {

        Long total = adminPlantMapper.selectCount(null);
        Integer start = (pageNum-1)*pageSize;

        List<PlanVo> plantList = adminPlantMapper.PlanList(commonName,scientificName,start,pageSize);

        PageResult pageResult = new PageResult(total,plantList);

        return pageResult;
    }

    /**
     * 回显
     *
     * @param id
     * @return
     */
    @Override
    public PlantInfoVo getPlantById(Integer id) {
        PlantInfoVo plantInfoVo = new PlantInfoVo();
        //查询植物信息
        Plant plant = adminPlantMapper.selectById(id);
        plantInfoVo.setPlant(plant);
        //查询植物家族
        if (plant.getFamilyId() != null) {
            PlantFamily plantFamily = plantFamilyMapper.selectPlantFamilyById(plant.getFamilyId());
            plantInfoVo.setPlantFamily(plantFamily);
        } else {
            plantInfoVo.setPlantFamily(new PlantFamily());
        }
        //查询植物属
        if (plant.getGenusId() != null) {
            PlantGenus plantGenus = genusMapper.selectPlantGenusById(plant.getGenusId());
            plantInfoVo.setPlantGenus(plantGenus);
        } else {
            plantInfoVo.setPlantGenus(new PlantGenus());
        }
        //查询植物培养
        if (plant.getBreedId() != null) {
            PlantBreed plantBreed = plantBreedMapper.selectPlantBreedById(plant.getBreedId());
            plantInfoVo.setPlantBreed(plantBreed);
        } else {
            plantInfoVo.setPlantBreed(new PlantBreed());
        }

        //查询植物生境
        if (plant.getEctopeId() != null) {
            PlantEcotope plantEcotope = plantEcotopeMapper.selectPlantEcotopeiById(plant.getEctopeId());
            plantInfoVo.setPlantEcotope(plantEcotope);
        } else {
            plantInfoVo.setPlantEcotope(new PlantEcotope());
        }

        //查询植物生长特征
        if (plant.getGrowthCharacteristicId() != null) {
            PlantGrowthCharacteristic plantGrowthCharacteristic = plantGrowthCharacteristicMapper.selectPlantGrowthCharacteristicById(plant.getGrowthCharacteristicId());
            plantInfoVo.setPlantGrowthCharacteristic(plantGrowthCharacteristic);
        } else {
            plantInfoVo.setPlantGrowthCharacteristic(new PlantGrowthCharacteristic());
        }
        return plantInfoVo;
    }

    /**
     * 修改
     *
     * @param plantInfoVo
     * @return
     */
    @Override
    public JsonRequest plantListUpdate(PlantInfoVo plantInfoVo) {
        //修改植物信息
//        Plant plant = new Plant();
//
//        if (plantInfoVo.getPlantBreed().getBreedId() != null) {
//            plantBreedMapper.updatePlantBreed(plantInfoVo.getPlantBreed());
//        } else {
//            PlantBreed plantBreed = plantInfoVo.getPlantBreed();
//            plantBreed.setPlantId(plantInfoVo.getPlant().getId());
//            plantBreedMapper.insertPlantBreed(plantBreed);
//            plant.setBreedId(plantInfoVo.getPlantBreed().getBreedId());
//        }
//        if (plantInfoVo.getPlantEcotope().getEcotopeId() != null) {
//            plantEcotopeMapper.updatePlantEcotope(plantInfoVo.getPlantEcotope());
//        } else {
//            PlantEcotope plantEcotope = plantInfoVo.getPlantEcotope();
//            plantEcotope.setPlantId(plantInfoVo.getPlant().getId());
//            plantEcotopeMapper.insertPlantEcotope(plantEcotope);
//            plant.setEctopeId(plantInfoVo.getPlantEcotope().getEcotopeId());
//        }
//        if (plantInfoVo.getPlantGrowthCharacteristic().getGrowthCharacteristicId() != null) {
//            plantGrowthCharacteristicMapper.updatePlantGrowthCharacteristic(plantInfoVo.getPlantGrowthCharacteristic());
//        } else {
//            PlantGrowthCharacteristic plantGrowthCharacteristic = plantInfoVo.getPlantGrowthCharacteristic();
//            plantGrowthCharacteristic.setPlantId(plantInfoVo.getPlant().getId());
//            plantGrowthCharacteristicMapper.insertPlantGrowthCharacteristic(plantInfoVo.getPlantGrowthCharacteristic());
//            plant.setGrowthCharacteristicId(plantInfoVo.getPlantGrowthCharacteristic().getGrowthCharacteristicId());
//        }
//
//        plant.setId(plantInfoVo.getPlant().getId());
//        plant.setCommonName(plantInfoVo.getPlant().getCommonName());
//        plant.setScientificName(plantInfoVo.getPlant().getScientificName());
//        plant.setDescription(plantInfoVo.getPlant().getDescription());
//        plant.setDateAdded(plantInfoVo.getPlant().getDateAdded());
//        plant.setLastUpdated(plantInfoVo.getPlant().getLastUpdated());

        String key = "plantplantListUpdate:" + plantInfoVo.getPlant().getId();

        RedisTemplate<String, Object> set = redisUtils.set(key, plantInfoVo);


        PlantInfoVo stringObjectSetOperations = (PlantInfoVo) redisUtils.get("plantplantListUpdate:" + plantInfoVo.getPlant().getId());
        //存储到redis中
        log.info("=======================存储到redis中的数据,{}", stringObjectSetOperations);

        return JsonRequest.success("200");
    }

    @Override
    public int insertPlantInfo(PlantInfoVo plantInfoVo) {


        Plant plant1 = new Plant();
        plant1.setId(plantInfoVo.getPlant().getId());
        plant1.setCommonName(plantInfoVo.getPlant().getCommonName());
        plant1.setScientificName(plantInfoVo.getPlant().getScientificName());
        plant1.setDateAdded(LocalDateTime.now());
        plant1.setLastUpdated(LocalDateTime.now());
        int plantCount = adminPlantMapper.insertPlant(plant1);
        if (plantCount < 1) {
            throw new ServiceException(RequestException.INSERT_ERROR);
        }
        //新增植物培育
        plantInfoVo.getPlantBreed().setPlantId(plantInfoVo.getPlant().getId());
        int plantBreedCount = plantBreedMapper.insertPlantBreed(plantInfoVo.getPlantBreed());
        if (plantBreedCount < 1) {
            throw new ServiceException(RequestException.INSERT_ERROR);
        }
        plant1.setBreedId(plantInfoVo.getPlantBreed().getBreedId());
        //新增植物生长特征
        int plantEcotopeCount = plantEcotopeMapper.insertPlantEcotope(plantInfoVo.getPlantEcotope());
        if (plantEcotopeCount < 1) {
            throw new ServiceException(RequestException.INSERT_ERROR);
        }
        plant1.setEctopeId(plantInfoVo.getPlantEcotope().getEcotopeId());

        //新增植物数据来源
        int plantGrowthCharacteristicCount = plantGrowthCharacteristicMapper.insertPlantGrowthCharacteristic(plantInfoVo.getPlantGrowthCharacteristic());
        if (plantGrowthCharacteristicCount < 1) {
            throw new ServiceException(RequestException.INSERT_ERROR);
        }
        plant1.setGrowthCharacteristicId(plantInfoVo.getPlantGrowthCharacteristic().getGrowthCharacteristicId());
        return adminPlantMapper.updatePlant(plant1);
    }

    @Override
    public int deletePlantById(Integer id) {
        Plant plant = adminPlantMapper.selectPlnt(id);

        int plantBreedCount = plantBreedMapper.deletePlantBreedById(plant.getBreedId());
        if (plantBreedCount < 1) {
            throw new ServiceException(RequestException.DELETE_ERROR);
        }
        int plantEcotopeCount = plantEcotopeMapper.deletePlantEcotopeById(plant.getEctopeId());
        if (plantEcotopeCount < 1) {
            throw new ServiceException(RequestException.DELETE_ERROR);
        }
        int plantGrowthCharacteristicCount = plantGrowthCharacteristicMapper.deletePlantGrowthCharacteristicById(plant.getGrowthCharacteristicId());
        if (plantGrowthCharacteristicCount < 1) {
            throw new ServiceException(RequestException.DELETE_ERROR);
        }
        return adminPlantMapper.deletePlant(id);
    }
}
